package net.wxxr.http.engine;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

import net.wxxr.http.config.HttpContans;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.request.HttpPostMutipleRequest;
import net.wxxr.http.request.HttpPutMutipleRequest;
import oauth.signpost.http.HttpParameters;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.FormBodyPart;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;



public class HttpPutMutipleEngine extends HttpInIdempotentEngine {

	public HttpPutMutipleEngine(HttpBaseRequest request) {
		super(request);
	}

	@Override
	protected void initTag() {
		TAG = "HttpPostMutipleEngine";
	}
	protected void initRequest() {
		requestBase = new HttpPut(baseRequest.getUrl());
	}
	
	@Override
	protected void setRequestParams() throws UnsupportedEncodingException {
		
		HttpPutMutipleRequest postRequest = (HttpPutMutipleRequest)baseRequest;
		MultipartEntity mpEntity = new MultipartEntity();
		
		//添加图片
		String fileKey = postRequest.getFileKey();
		File file = postRequest.getFile();
		mpEntity.addPart(fileKey, new FileBody(file));
		//添加图片以外的额外参数
		String contentType = postRequest.getContentType();
		if(HttpContans.CONTENT_TYPE_JSON.equals(contentType)){
			String bodyData = postRequest.getBodyData();
			if(null != bodyData){
				mpEntity.addPart("data", new StringBody(bodyData,Charset.forName(HttpContans.CHARSET_GBK)));
			}
			
		}else if(HttpContans.CONTENT_TYPE_XML.equals(contentType)){
			
		}else if(HttpContans.CONTENT_TYPE_MAP.equals(contentType)){
			HttpParameters bodyParams = postRequest.getBodyParams();
			if(null != bodyParams){
				Set<String> keySet = bodyParams.keySet();
				Iterator<String> keyIt = keySet.iterator();
				while(keyIt.hasNext()) {
					String key = keyIt.next();
					String value = bodyParams.getFirst(key);
					mpEntity.addPart(key, new StringBody(value));
				}
			}
			
		}
		
		
		/**
		 * 服务端接受数据
		 * 
            public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                boolean isHaveData = ServletFileUpload.isMultipartContent(request);
                if (isHaveData) {
                        FileItemFactory factory = new DiskFileItemFactory();
                        ServletFileUpload upload = new ServletFileUpload(factory);
                        try {
                                List items = upload.parseRequest(request);
                                Iterator iter = items.iterator();
                                while (iter.hasNext()) {
                                        FileItem item = (FileItem) iter.next();
                                        if (item.isFormField()) {
                                                // 普通文本信息处理
                                                String paramName = item.getFieldName();
                                                String paramValue = item.getString();
                                                System.out.println(paramName + ":" + paramValue);
                                        } else {
                                                // 上传文件信息处理
                                                String fileName = item.getName();
                                                byte[] data = item.get();
                                                String filePath = getServletContext().getRealPath(
                                                                "/files")
                                                                + "/" + fileName;
                                                FileOutputStream fos = new FileOutputStream(filePath);
                                                fos.write(data);
                                                fos.close();
                                        }
                                }
                        } catch (FileUploadException e) {
                                e.printStackTrace();
                        }
                        
                }

		 */
		
    	((HttpPut) requestBase).setEntity(mpEntity); 
	}
	
}
